<?php

namespace App\Http\Controllers;

use App\Models\AdminUser;
use App\Services\EasyWechatEven;
use App\Services\StatusNumber;
use App\Services\UserInfoService;
use App\Tools\JsonTools;
use EasyWeChat\Factory;
use Godruoyi\Snowflake\Snowflake;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;

class EasyWechatController extends Controller
{


    /**
     * EasyWechatController constructor.
     */
    public $app;

    public function __construct()
    {
        //配置属性
        $config = config('easyWechat.mode');
        $this->app = Factory::officialAccount($config);
    }

    /**
     * @Notes:连接wechat
     * @Author:Planb,
     * @Date: 2022/11/28,
     * @Time: 8:36,
     */
    public function linkWeChat()
    {
        $this->menu();
        $this->message();
        return $this->app->server->serve();
    }

    public function menu()
    {
        $message = $this->app->server->getMessage();
        Log::info($message);
        if (isset($message['FromUserName'])) {
            $openId = $message['FromUserName'];
            if (!$openId) {
                return '请重新扫码认证';
            }
        }
        $buttons = [
            [
                "type" => "view",
                "name" => "授权认证",
                "url" => "http://www.cyjyyds.cn/api/authorized_login?open_id=$openId"
            ],
            [
                "name" => "菜单",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "搜索",
                        "url" => "http://www.soso.com/"
                    ],
                    [
                        "type" => "view",
                        "name" => "视频",
                        "url" => "http://v.qq.com/"
                    ],
                    [
                        "type" => "click",
                        "name" => "赞一下我们",
                        "key" => "V1001_GOOD"
                    ],
                ],
            ],
        ];
        $this->app->menu->create($buttons);
    }

    /**
     * @Notes:创建二维码
     * @Author:Planb,
     * @Date: 2022/11/28,
     * @Time: 8:44,
     */
    public function qr()
    {
        $id = (new Snowflake())->id();
        $result = $this->app->qrcode->forever($id);// 或者 $app->qrcode->forever("foo");
        $url = $this->app->qrcode->url($result['ticket']);
        return [
            'url' => "$url",
            'id' => $id
        ];
    }


    /**
     * @Notes:消息类型
     * @Author:Planb,
     * @Date: 2022/12/1,
     * @Time: 10:16,
     */
    public function message()
    {
        $this->app->server->push(function ($message) {
            switch ($message['MsgType']) {
                case 'event':
                    $result = (new EasyWechatEven())->even($message);
                    return $result;
                    break;
                case 'text':
                    return '收到文字消息';
                    break;
                case 'image':
                    return '收到图片消息';
                    break;
                case 'voice':
                    return '收到语音消息';
                    break;
                case 'video':
                    return '收到视频消息';
                    break;
                case 'location':
                    return '收到坐标消息';
                    break;
                case 'link':
                    return '收到链接消息';
                    break;
                case 'file':
                    return '收到文件消息';
                // ... 其它消息
                default:
                    return '收到其它消息';
                    break;
            }
        });

    }


    /**
     * @Notes:用户授权认证
     * @Author:Planb,
     * @Date: 2022/11/28,
     * @Time: 14:16,
     */
    public function userAuth()
    {
        $username = request()->post('username');
        $password = request()->post('password');
        $openId = request()->post('open_id');
        $bindResult = EasyWechatEven::bindOpenId($username, $password, $openId);
        switch ($bindResult) {
            case StatusNumber::REPEATBIND:
                return '请勿重新绑定';
                break;
            case StatusNumber::BINDSUCCESS:
                return '绑定成功';
                break;
            case StatusNumber::BINDFAIL:
                return '绑定失败';
                break;
            case StatusNumber::USERNAMEORPASSWORDERROR:
                return '用户名或密码错误';
                break;
        }
    }

    /**
     * @Notes:根据open_id获取用户信息
     * @Author:Planb,
     * @Date: 2022/11/28,
     * @Time: 19:53,
     */
    public function byOpenIdGetUserInfo(){
        $wxOpenId = request()->post('open_id');
        $result = UserInfoService::getUserInfo($wxOpenId);
        if ($result){
            return JsonTools::JsonSuccess($result,200,'ok');
        }else{
            return JsonTools::JsonError([],3001,'登录失败');
        }
    }

}
